//绑定邮箱
var emailsign = true; //邮箱标记
function bindEmail(emailVal){
    var src= 'this.src="'+imgCode+'" + Math.random()';
    var docSrc = 'document.getElementById("imgCodes").src="'+imgCode+'" + Math.random()';
    var str = "";
    str += "<div class='bind'>";
    str += "<form method='post' name='eForm' id='eForm' onsubmit='return false'>";
    str += "<div  class='bindtip'><span>注：系统将发送验证邮件到您设置的邮箱;点击邮箱中的链接即可完成验证;</span></div>";
    str += "<div class='binCon'>";

    str += "<dl>";
    str += "<dt>绑定邮箱<span class='asterisk'>*</span></dt>";
    str += "<dd>";
    str += "<span><input name='email' id='email' type='text' value='"+emailVal+"'  placeholder='请输入邮箱' class='inptext' ></span>";
    str += "<span class='errinfo erremail'></span>";
    str += "</dd>";
    str += "</dl>";

    str += "<dl>";
    str += "<dt>验证码<span class='asterisk'>*</span></dt>";
    str += "<dd>";
    str += "<span><input name='code' id='code' type='text'  placeholder='请输入验证码' class='code' ></span>";
    str += "<span><img id='imgCodes' src='" + imgCode + "' onclick='"+ src + "' /></span>";
    str += "<span class='change'><a onclick='"+ docSrc + "'>换一换</a></span>";
    str += "<span class='errinfo errcode'></span>";
    str += "</dd>";
    str += "</dl>";

    str += "</div>";
    str += "</form>";
    str += "</div>";

    layer.confirm('邮箱绑定', {
            skin: 'bindSkin',
            move:false,
            title:'邮箱绑定',
            area: ['600px','300px'],
            content:str,
            btn: ['发送验证邮箱','取消'] //按钮
        }, function(){
            var eForm = $("#eForm");
            var email = str_trim(eForm.find("#email").val());
            //邮箱
            if(email==""){
                eForm.find('.erremail').html('绑定邮箱不能为空！').show();
                eForm.find("#email").focus();
                eForm.find("#email").val(email);
                return false;
            }else{
                var regu =/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
                if (!regu.test(email)){
                    eForm.find('.erremail').html('请输入正确的邮箱地址！').show();
                    eForm.find("#email").focus();
                    eForm.find("#email").val(email);
                    return false;
                }else{
                    eForm.find('.erremail').hide();
                }
            }
            //验证码
            var code = str_trim(eForm.find("#code").val());
            if(code==""){
                eForm.find('.errcode').html('验证码不能为空！').show();
                eForm.find("#code").focus();
                eForm.find("#code").val(code);
                return false;
            }
            $.post('/common/ajax_code',{'_token':_token,'code':code},function(data) {
                if(data == 'true'){
                    if(emailsign){
                        emailsign = false;
                        $.ajax({
                            url:'/personal/email_send',
                            dataType:"html",
                            data: "_token="+_token+"&email="+email+"&code="+code,
                            type:"POST",
                            success: function(data){
                                if(data){
                                    var str = '';
                                    str += "<div class='sendsucc'>";
                                    str += "<p class='sendto'>验证邮件已发送至"+email+"</p>";
                                    str += "<p>请尽快登录您的邮箱点击邮件中的验证链接完成绑定;</p>";
                                    str += "<div class='nomali'>";
                                    str += "<div class='mailinfo'>";
                                    str += "<div class='maiocon'>";
                                    str += "<p><strong>没收到邮件？</strong></p>";
                                    str += "<p>1、请检查您的垃圾邮件或者广告邮件夹，邮件有可能被误认为垃圾或者广告邮件；</p>";
                                    str += "<p>2、如果仍然长时间没有收到邮件，建议您重新发送邮件？</p>";
                                    str += "</div>";
                                    str += "</div>";
                                    str += "</div>";
                                    $(".bindSkin").find('.layui-layer-content').html(str);
                                    $(".bindSkin").find('.layui-layer-btn').hide();
                                }
                            },
                            error:function(){
                                var str = '';
                                str += "<div class='sendsucc'>";
                                str += "<p class='sendto senderr'>邮件发送至"+email+"失败</p>";
                                str += "<p>请检查您的发送邮件是不是验证或者有效;</p>";
                                str += "<div class='nomali'>";
                                str += "<div class='mailinfo'>";
                                str += "<div class='maiocon'>";
                                str += "<p><strong>温馨提示？</strong></p>";
                                str += "<p>1、请检查您发送邮件，邮件有可能被误认为您发送的邮件失效；</p>";
                                str += "<p>2、如果仍然长时间没有收到邮件，建议您重新发送邮件？</p>";
                                str += "</div>";
                                str += "</div>";
                                str += "</div>";
                                str += "</div>";
                                $(".bindSkin").find('.layui-layer-content').html(str);
                                $(".bindSkin").find('.layui-layer-btn').hide();
                            }
                        });
                    }
                }else{
                    eForm.find('.errcode').html('验证码错误！').show();
                }
            });
        }
    );
}

/**
 * 绑定手机
 * @param mobileVal
 */
var mobilesign = true;//手机标记
function bindMobile(mobileVal){
    var str = "";
    str += "<div class='bind'>";
    str += "<form method='post' name='mForm' id='mForm' onsubmit='return false'>";
    str += "<div  class='bindtip'><span>注：点击绑定手机后,修改用户信息里面的手机;</span></div>";
    str += "<div class='binCon'>";

    str += "<dl>";
    str += "<dt>手机号码<span class='asterisk'>*</span></dt>";
    str += "<dd>";
    str += "<span><input name='mobile' id='mobile' type='text' value='"+mobileVal+"'  placeholder='请输入手机号码' class='inptext' ></span>";
    str += "<span class='errinfo errmobile'></span>";
    str += "</dd>";
    str += "</dl>";

    str += "<dl>";
    str += "<dt>验证码<span class='asterisk'>*</span></dt>";
    str += "<dd>";
    str += "<span><input name='mcode' id='mcode' type='text' value=''  placeholder='请输入验证码' class='code' ></span>";
    str += "<span><input name='mobcode'  class='sendcode' id='mobcode' type='button' onclick='getcode(\"#mForm\",\"#mobile\",1)' value='免费发送验证码' ></span>";
    str += "<span class='errinfo errcode'></span>";
    str += "</dd>";
    str += "</dl>";

    str += "<dl style='margin-top:5px'>";
    str += "<dt></dt>";
    str += "<dd>";
    str += "<span class='errinfo errsms'></span>";
    str += "</dd>";
    str += "</dl>";

    str += "</div>";
    str += "</form>";
    str += "</div>";

    layer.confirm('手机绑定', {
            skin: 'bindSkin',
            move:false,
            title:'更换手机',
            area: ['600px','300px'],
            content:str,
            btn: ['立即绑定','取消'] //按钮
        }, function(){
            getcode("#mForm","#mobile",0);
            //验证码
            var mForm = $("#mForm");
            var mobile = str_trim(mForm.find("#mobile").val());
            var mcode = str_trim(mForm.find("#mcode").val());
            if(mcode != ''){
                $.post('/personal/ajax_job_sms',{'_token':_token,'mcode':mcode,'mobile':mobile},function(data) {
                    if(data == 'true'){
                        if(mobilesign){
                            mobilesign=false;
                            $.post('/personal/bind_mobile',{'_token':_token,'mobile':mobile,'mcode':mcode},function(data) {
                                if(data.status == '0'){
                                    layer.msg(data.msg,{icon:6,time:1500});
                                    location='/personal/set_account';
                                }else{
                                    layer.msg(data.msg,{icon:5,time:1500});
                                }
                            });
                        }
                    }else{
                        mForm.find('.errcode').html('手机号码和验证号码不匹配！').show();
                    }
                });
            }else{
                mForm.find('.errcode').html('请输入短信验证码！').show();
            }
        }
    );
}
/**
 * 获取验证码
 * @param oForm
 * @param oMobile
 * @param otype
 */
var mobileTime = '';
function getcode(oForm,oMobile,otype){
    var mForm = $(oForm);
    var mobile = str_trim(mForm.find(oMobile).val());
    //手机
    if(mobile==""){
        mForm.find('.errmobile').html('手机号码不能为空！').show();
        mForm.find(oMobile).focus();
        mForm.find(oMobile).val(mobile);
        mForm.find('.errsms').hide();
    }else{
        var regu =/^(\d{3,4}[-])?(1[3-9]{1}[0-9]{9}$)/;
        if (!regu.test(mobile)){
            mForm.find('.errmobile').html('请输入正确的手机号码！').show();
            mForm.find(oMobile).focus();
            mForm.find(oMobile).val(mobile);
            mForm.find('.errsms').hide();
        }else{
            mForm.find('.errmobile').hide();
            mForm.find(oMobile).val(mobile);
            if(otype == 1){
                $.post('/personal/ajax_job_code',{'_token':_token,'mobile':mobile},function(data){
                    if(data.status==2){
                        //验证手机号码是不是存在
                        mForm.find('.errsms').html('此手机号码已经被绑定').show();
                    }else if(data.status==1){
                        clearInterval(mobileTime);
                        //获取验证码成功
                        var iNum=60;
                        mobileTime = setInterval(function(){
                            iNum -= 1;
                            mForm.find('#mobcode').attr("disabled",true).val('('+iNum+')秒后重新获取').css({"background":"#ccc","color":"#333"});
                            if(iNum <=0){
                                mForm.find('#mobcode').attr("disabled",false).val('免费发送验证码').css({"background":"#6FA936","color":"#fff"});
                                clearInterval(mobileTime);
                            }
                        },1000);
                        mForm.find('.errsms').hide();
                    }else if(data.status==0){
                        //验证码获取失败
                        mForm.find('.errsms').html('一个号码一分钟之内只能发一次').show();
                    }
                });
            }
        }
    }



}